import { createRouter, createWebHistory } from 'vue-router'

import navbarConfig from './navbar.config'
import pageConfig from './page.config'

import MainLayout from '@/layout/main.vue'
import PageLayout from '@/layout/page.vue'
import {getToken} from "@/utils/auth";
const whiteList = ['/login']

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
        path: '',
        component: MainLayout,
        redirect: navbarConfig[0] || '/404',
        children: navbarConfig
      }, {
        path: '',
        component: PageLayout,
        children: pageConfig
      }, {
        name: 'login',
        path: '/login',
        component: () => import("@/views/Login.vue")
      },
      {
        path : '/msg',
        component : () => import('@/views/msg/index.vue'),
      }
  ]
})


//登录验证
router.beforeEach((to, from, next) =>{
  if (getToken()){
    //已经登录
      next()
  }else {
    //没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login`) // 否则全部重定向到登录页
    }
  }
})

export default router
